<template>
  <div class="app-container">
    <div id="head">
      <div class="block">
        <el-button type="primary" icon="el-icon-plus" @click="addad('ruleForm')">添加荣誉</el-button>
        <el-button v-show="delshow" type="danger" icon="el-icon-delete" @click="delselect">批量删除</el-button>
      </div>
      <el-dialog top="10vh" title="添加荣誉" width="35%" :visible.sync="dialogFormVisible" :before-close="handleClose">
        <el-form ref="ruleForm" :model="form">
          <el-form-item label="时间" style="margin-bottom: 10px" :label-width="formLabelWidth" prop="name">
            <el-date-picker
              v-model="form.honorTime"
              type="date"
              placeholder="选择日期"
            />
          </el-form-item>
          <el-form-item label="荣誉描述" :label-width="formLabelWidth">
            <el-input v-model="form.honorText" class="input" type="textarea" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleCancel1">取 消</el-button>
          <el-button type="primary" @click="promit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-table
      :data="list"
      border
      style="width: 98%;margin-top: 20px"
      @selection-change="handleSelectionChange1"
    >
      <el-table-column
        align="center"
        type="selection"
        width="100"
      />
      <el-table-column
        type="index"
        align="center"
        label="序号"
        width="200"
      />
      <el-table-column
        align="center"
        prop="honorTime"
        label="时间"
        width="320"
      />
      <el-table-column
        align="center"
        prop="honorText"
        label="荣誉描述"
        width="600"
      />
      <el-table-column
        align="center"
        prop="act"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            id="sty4"
            plain
            size="mini"
            class="ml-1"
            @click="changed(scope.row.id)"
          ><span>修改</span></el-button>
          <el-popconfirm
            title="确定删除该荣誉吗？"
            @onConfirm="del(scope.row.id)"
          >
            <el-button
              id="sty3"
              slot="reference"
              type="danger"
              size="mini"
              class="ml-1"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--修改信息-->
    <el-dialog top="9vh" title="修改荣誉信息" width="35%" :visible.sync="dialogFormVisible1" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="时间" style="margin-bottom: 10px" :label-width="formLabelWidth" prop="name">
          <el-date-picker
            v-model="shopdata.honorTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="荣誉描述" :label-width="formLabelWidth">
          <el-input v-model="shopdata.honorText" class="input" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="ml-5" @click="handleCancel2">取 消</el-button>
        <el-button type="primary" class="ml-auto" @click="save(shopdata.goodsId)">修改</el-button>
      </div>

    </el-dialog>
    <!--分页-->
    <el-pagination
      background
      :current-page="pagee.pageNum"
      :page-sizes="[10, 50, 100, 200]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagee.total"
      class="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @prev-click="handlePrevClick"
      @next-click="handleNextClick"
    />

  </div>
</template>
<script>
import { getHonorList, addOrUpdHonor, getHonorInfo, delHonor } from '@/api/shop'
export default {
  name: 'SchoolHonor',
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      delshow: false,
      pagee: '',
      ids: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      shopdata: '',
      form: {
        honorTime: '',
        honorText: ''
      },
      formLabelWidth: '80px',
      list: [],
      num: ''
    }
  },
  created() {
    this.change()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    addad() { // 新增
      this.dialogFormVisible = true
      this.form.honorText = ''
      this.form.honorTime = ''
    },
    handleCancel2() {
      this.dialogFormVisible1 = false
    },
    handleCancel1() {
      this.$refs.ruleForm.clearValidate()
      this.dialogFormVisible = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.change()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.change()
      console.log(`当前页: ${val}`)
    },
    handlePrevClick() {
      this.pageNum--
      this.change()
    },
    handleNextClick() {
      this.pageNum++
      this.change()
    },
    handleSelectionChange1(val) {
      console.log(val.length)
      this.ids = []
      if (val.length > 0) {
        val.forEach(res => {
          this.ids.push(res.id)
          console.log(this.ids)
        })
      }
      this.delshow = val.length > 0
    },
    // 批量删除
    delselect() {
      const data = {
        id: this.ids
      }
      delHonor(data).then(res => {
        console.log(res)
        if (res.success) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.change()
        }
      })
    },
    // 修改
    changed(id) {
      this.show3 = false
      this.dialogFormVisible1 = true
      this.shopdata = []
      const data = {
        id: id
      }
      getHonorInfo(data).then(res => {
        if (res.success) {
          this.shopdata = res.dataMap
        }
      })
    },
    // 修改保存
    save() {
      if (this.shopdata.honorText === '' || this.shopdata.honorTime === '') {
        this.$message('请填写完整信息')
        return
      } else {
        this.shopdata.honorTime = this.datatime(this.renderTime(this.shopdata.honorTime))
        addOrUpdHonor(this.shopdata).then(res => {
          if (res.success) {
            console.log(res)
            this.dialogFormVisible1 = false
            this.change()
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message.error('操作失败')
          }
        })
      }
    },
    // 删除
    del(id) {
      const data = {
        id: []
      }
      data.id = []
      data.id.push(id)
      delHonor(data).then(res => {
        if (res.success) {
          this.change()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    // 商品列表的展示
    change() {
      const data = {
      }
      getHonorList(data, this.pageNum, this.pageSize).then(res => {
        console.log(res)
        this.pagee = res.dataMap
        this.list = res.dataMap.list
      })
    },
    // 查询
    search() {
      this.pageNum = 1
      this.change()
    },
    renderTime(date) {
      var dateee = new Date(date).toJSON()
      return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    },
    datatime(value) {
      var data = new Date(value)
      var month = data.getMonth() < 9 ? '0' + (data.getMonth() + 1) : data.getMonth() + 1
      var date = data.getDate() <= 9 ? '0' + data.getDate() : data.getDate()
      return data.getFullYear() + '-' + month + '-' + date
    },
    // 新增提交
    promit() {
      if (this.form.honorText === '' || this.form.honorTime === '') {
        this.$message('请填写完整信息')
        return
      } else {
        this.form.honorTime = this.datatime(this.renderTime(this.form.honorTime))
        addOrUpdHonor(this.form).then(res => {
          if (res.success) {
            this.dialogFormVisible = false
            this.change()
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          } else {
            this.$message.error('操作失败')
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.page{
  margin-top: 20px;
  margin-right: 20px;
  float: right;
}
.input{
  width: 400px;
}
.block{
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

#sty3{
  border:1px solid #ff7f5b;
}

element.style {
  margin-top: 10vh;
}

.el-table td div {
  margin-left: 10px;
}

form {
  /*width: 80%;*/
  margin: auto;
}

#sty4 {
  border:1px solid #ff7f5b;
  color: #ff7f5b;
}

#sty2 {
  /*display: none;*/
}

.el-input[data-v-da9eedb6] {
  width: 17%;
  float: left;
  overflow: hidden;
}

#sty1, #sty2, #sty3, #sty4 {
  margin-right: 0;
}

#head {
  width: 100%;
  height: 30px;
}

.el-table .cell {
  padding-left: 42px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>

